package com.yf.repo.modules.repo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.base.utils.BeanMapper;
import com.yf.repo.modules.repo.dto.ext.ChapterExtDTO;
import com.yf.repo.modules.repo.entity.RepoChapter;
import com.yf.repo.modules.repo.mapper.RepoChapterMapper;
import com.yf.repo.modules.repo.service.RepoChapterService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* <p>
* 题库章节业务实现类
* </p>
*
* @author 聪明笨狗
* @since 2020-12-01 10:44
*/
@Service
public class RepoChapterServiceImpl extends ServiceImpl<RepoChapterMapper, RepoChapter> implements RepoChapterService {


    @Override
    public List<ChapterExtDTO> listByRepo(String repoId) {
        List<ChapterExtDTO> respList = baseMapper.listByRepo(repoId);
        return respList;
    }

    @Override
    public void saveAll(String repoId, List<ChapterExtDTO> list) {

        //最终要保存的列表
        List<RepoChapter> chapterList = new ArrayList<>();

        //已存在的标签列表
        List<String> existsIds = this.findExistsIds(repoId);

        if(!CollectionUtils.isEmpty(list)){

            int sort = 1;

            for(ChapterExtDTO item: list){

                // 复制属性
                RepoChapter chapter = new RepoChapter();
                BeanMapper.copy(item, chapter);
                chapter.setRepoId(repoId);
                chapter.setSort(sort);

                // 留下的就是被删除的
                String id = item.getId();
                if(existsIds.contains(id)){
                    existsIds.remove(id);
                }

                chapterList.add(chapter);

                sort++;
            }

            //保存标签列表
            if(!CollectionUtils.isEmpty(chapterList)) {
                this.saveOrUpdateBatch(chapterList);
            }

            //删除已移除
            if(!existsIds.isEmpty()){
                this.removeByIds(existsIds);
            }

        }else{
            // 全部删掉了
            QueryWrapper<RepoChapter> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(RepoChapter::getRepoId, repoId);
            this.remove(wrapper);
        }

    }

    @Override
    public Map<String, String> findMap(String repoId) {
        QueryWrapper<RepoChapter> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RepoChapter::getRepoId, repoId);


        List<RepoChapter> list = this.list(wrapper);

        Map<String, String> map = new HashMap<>(16);

        if(!CollectionUtils.isEmpty(list)){
            for(RepoChapter item: list){
                map.put(item.getTitle(), item.getId());
            }
        }
        return map;
    }

    /**
     * 查找已存在的ID列表
     * @param repoId
     * @return
     */
    @Override
    public List<String> findExistsIds(String repoId) {

        QueryWrapper<RepoChapter> wrapper = new QueryWrapper();
        wrapper.lambda().eq(RepoChapter::getRepoId, repoId);
        List<RepoChapter> list = this.list(wrapper);

        List<String> ids = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (RepoChapter item : list) {
                ids.add(item.getId());
            }
        }
        return ids;
    }

}
